local skel = fk.CreateSkill{
  name = "sj8__qiuli",
}

Fk:loadTranslationTable{
  ["sj8__qiuli"] = "求鲤",
  [":sj8__qiuli"] = "出牌阶段限一次，你可以令一名角色分配牌堆顶两张牌，然后对其造成1点冰冻伤害。若弃置了获得的牌，此技能视为未发动过且本回合弃牌数+1。",

  ["#sj8__qiuli"] = "求鲤：你可以令一名角色分配牌堆顶两张牌，然后对其造成1点冰冻伤害",
  ["#sj8__qiuli-give"] = "求鲤：你可以将这些牌分配给任意角色，点“取消”自己保留",
  ["@sj8__qiuli-turn"] = "求鲤",


  ["#diy__ice_damage_skill"] = "冰冻伤害",
  ["#diy__ice_damage_skill-invoke"] = "你可以防止对 %dest 造成的冰冻伤害，改为依次弃置其两张牌",
  ["#diy__ice_damage_skill_num-invoke"] = "你可以防止对 %dest 造成的冰冻伤害，改为依次弃置其 %arg 张牌",

  ["$sj8__qiuli1"] = "解衣卧沂层冰上，双鲤鱼跃擢清漪。",
  ["$sj8__qiuli2"] = "一日三公不足荣，忆昔卧冰求鲤时。",
}

skel:addEffect("active", {
  anim_type = "defensive",
  prompt = "#sj8__qiuli",
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local ids = room:getNCards(2) --遗计

    while not target.dead do
      local tos, cards = room:askToChooseCardsAndPlayers(target, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = #ids,
        targets = room.alive_players,
        pattern = tostring(Exppattern{ id = ids }),
        skill_name = skel.name,
        prompt = "#sj8__qiuli-give",
        cancelable = true,
        expand_pile = ids,
      })
      if #tos > 0 and #cards > 0 then
        for _, id in ipairs(cards) do
          table.removeOne(ids, id)
        end
        room:moveCardTo(cards, Card.PlayerHand, tos[1], fk.ReasonGive, skel.name, nil, false, target)
        if #ids == 0 then break end
      else
        room:moveCardTo(ids, Card.PlayerHand, target, fk.ReasonGive, skel.name, nil, false, target)
        break
      end
    end
    if not target.dead then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        damageType = fk.IceDamage,
        skillName = skel.name,
        extra_data = {
          ice_discard_num = player:getMark("@sj8__qiuli-turn"),
          afterfun = function (data)
            if table.find(data.extra_data.ice_discards,function (id)
              return table.contains(ids,id)
            end) then
              room:addPlayerMark(player,"@sj8__qiuli-turn")
              player:setSkillUseHistory(skel.name)
            end
          end,
        },
      }
    end
  end,
})

return skel